package cn.harry.modular.sys.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.common.utils.SecurityUtils;
import cn.harry.modular.sys.domain.SysMenu;
import cn.harry.modular.sys.service.SysMenuService;
import cn.harry.modular.sys.vo.RouterVo;
import cn.harry.modular.sys.vo.TreeSelect;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SysMenuController Description:
 *
 * @author honghh Date 2019/10/17 10:00 Copyright (C) www.tech-harry.cn
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/menu")
@Tag(name = "sys-menu 系统菜单管理")
public class SysMenuController {

	private final SysMenuService sysMenuService;

	@Operation(summary = "list 获取菜单列表")
	@GetMapping("list")
	@PreAuthorize("@ss.hasPermission('system:menu:list')")
	public R<List<SysMenu>> list(SysMenu menu) {
		List<SysMenu> menus = sysMenuService.selectMenuList(menu,
				SecurityUtils.getUserId());
		return R.success(menus);
	}

	@Operation(summary = "{id} 根据菜单ID获取详细信息")
	@PreAuthorize("@ss.hasPermission('system:menu:query')")
	@GetMapping("/{id}")
	public R<SysMenu> info(@PathVariable("id") Long id) {
		SysMenu menus = sysMenuService.getById(id);
		return R.success(menus);
	}

	@Operation(summary = "getRouters  获取路由信息")
	@GetMapping("getRouters")
	public R<List<RouterVo>> getRouters() {
		List<SysMenu> menus = sysMenuService
				.selectMenuTreeByUserId(SecurityUtils.getSysUser().getId());
		return R.success(sysMenuService.buildMenus(menus));
	}

	@Operation(summary = "treeselect 获取菜单下拉树列表")
	@GetMapping(value = "/treeselect")
	public R<List<TreeSelect>> treeselect(SysMenu menu) {
		List<SysMenu> menus = sysMenuService.selectMenuList(menu,
				SecurityUtils.getSysUser().getId());
		return R.success(sysMenuService.buildMenuTreeSelect(menus));
	}

	@Operation(summary = "roleMenuTreeselect/{roleId} 加载对应角色菜单列表树")
	@GetMapping(value = "/roleMenuTreeselect/{roleId}")
	public R<Map<String, Object>> roleMenuTreeselect(
			@PathVariable("roleId") Long roleId) {
		List<SysMenu> menus = sysMenuService
				.selectMenuList(SecurityUtils.getSysUser().getId());
		Map<String, Object> map = new HashMap<>();
		map.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
		map.put("menus", sysMenuService.buildMenuTreeSelect(menus));
		return R.success(map);
	}

	@Operation(summary = "新增菜单")
	@PreAuthorize("@ss.hasPermission('system:menu:add')")
	@SysLog(title = "菜单管理", businessType = BusinessType.INSERT)
	@PostMapping
	public R<Integer> create(@RequestBody SysMenu menu) {
		return sysMenuService.save(menu) ? R.success() : R.failed();
	}

	@Operation(summary = "修改菜单")
	@PreAuthorize("@ss.hasPermission('system:menu:edit')")
	@SysLog(title = "菜单管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public R<Integer> update(@RequestBody SysMenu menu) {
		return sysMenuService.updateById(menu) ? R.success() : R.failed();
	}

	@Operation(summary = "{id} 删除菜单")
	@PreAuthorize("@ss.hasPermission('system:menu:remove')")
	@SysLog(title = "菜单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public R<Integer> delete(@PathVariable("id") Long id) {
		return sysMenuService.deleteById(id) ? R.success() : R.failed();
	}

}
